package com.heima.springai.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.heima.springai.repository.IChatHistoryService;
import com.heima.springai.entity.vo.MessageVo;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 噤玥
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/ai/history")
public class ChatHistoryController {
    @Resource
    private IChatHistoryService chatHistoryService;

    @GetMapping("{type}")
    public List<String> getHistoryId(@PathVariable("type") String type) {
        return chatHistoryService.getIdList(type);
    }

    @GetMapping("{type}/{chatId}")
    public List<MessageVo> getHistoryChat(@PathVariable("type") String type, @PathVariable("chatId") String chatId) throws JsonProcessingException {
//        List<MessageVo> list=List.of();
//        chatHistoryService.saveHistoryMessages(list, chatId, type);
        List<MessageVo> historyMessages = chatHistoryService.getHistoryMessages(chatId, type);
        if (!historyMessages.isEmpty()) {
            return historyMessages;
        }
        return List.of();
//        List<Message> messages = chatMemory.get(chatId);
//         list = messages.stream().map(MessageVo::new).toList();
//        if (list.isEmpty()){
//            return List.of();
//        }
//        chatHistoryService.saveHistoryMessages(list, chatId, type);
//        return list;
    }

}
